﻿using Dapper;
using Domain.Entity.System_setting;
using Domain.IRepository.System_setting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;

namespace EFCore.Repository.System_setting
{
    [Dependency(ServiceLifetime.Scoped)]
    public class CompanyRepository : ICompanyRepository
    {

        private MyDbContext myDbContext { get; set; }

        public CompanyRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public int AddCompany(Info_company info_Company)
        {
            info_Company.Comp_id = Convert.ToInt64(info_Company.Comp_id);
            myDbContext.Info_company.Add(info_Company);
            return myDbContext.SaveChanges();
        }

        public int UpdCompany(Info_company company)
        {
            string sql = $@"UPDATE dbo.Info_company SET Comp_logo='{company.Comp_logo}', Comp_name='{company.Comp_name}', Comp_code='{company.Comp_code}', Contact_by='{company.Contact_by}', Contact_phone='{company.Contact_phone}', Comp_address='{company.Comp_address}', Comp_remark='{company.Comp_Remark}', Update_by='{company.Update_by}', Update_time='{company.Update_time}'
            WHERE Comp_id='{company.Comp_id}'";
            return myDbContext.Database.GetDbConnection().Execute(sql);
        }

        public Info_company GetById()
        {
            string sql = "SELECT * FROM dbo.Info_company WHERE Is_del=0";
            return myDbContext.Database.GetDbConnection().Query<Info_company>(sql).ToList()[0];
        }
    }
}
